//
//  HQDate.m
//  CommonKit
//
//  Created by guohongqi on 2021/7/8.
//

#import "HQDate.h"

@implementation HQDate

+ (NSTimeInterval)getCurrentTimestamp {
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

    NSTimeInterval timestamp =[dat timeIntervalSince1970];
    return timestamp;
}

+ (NSTimeInterval)getCurrentMsecTimestamp{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

    NSTimeInterval timestamp =[dat timeIntervalSince1970] * 1000;
    return timestamp;
}

#pragma mark - 时间戳转Date
+ (NSDate *)getDateFromTimeIntervalString:(NSString*)timeStr{
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeStr doubleValue]/1000];
    return date;
}

+ (NSDate *)getDateFromTimeInterval:(long)timeStr
{
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeStr/1000];
    return date;
}

#pragma mark - Date转时间字符串
+ (NSString *)getTimeWithFormatter:(NSString *)formatter date:(NSDate *)date{
    //yyyy-MM-dd HH:mm:ss
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    //设置时间格式为公历时间
    NSCalendar *calendar =[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [dateFormatter setCalendar:calendar];
    //使用手机设置里面的默认设置
    //    [dateFormatter setLocale:[NSLocale systemLocale]];
    //设置时间转换成字符串的格式
    [dateFormatter setDateFormat:formatter];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;
}

#pragma mark - 获取当前时间
+ (NSString *)getCurrentDate
{
    NSDate *curDate = [NSDate date];
    NSDate *zeroDate = [self getZeroDateWithDate:curDate];
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString * curTime = [formater stringFromDate:zeroDate];
    return curTime;
}

+ (NSString *)getCurrentTime
{
    NSDate *curDate = [NSDate dateWithTimeIntervalSince1970:[self getCurrentTimestamp]];
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString * curTime = [formater stringFromDate:curDate];
    return curTime;
}


#pragma mark - 从时间戳转换日期字符串
// @"yyyy-MM-dd HH:mm:ss"
+ (NSString *)dateTransformFromTimestamp:(long)timestamp formater:(NSString *)formaterStr{
    NSDate *zeroDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:formaterStr];
    NSString * curTime = [formater stringFromDate:zeroDate];
    return curTime;
}


#pragma mark -- 获取传入时间的当天的0时0分0秒
+(NSDate *)getZeroDateWithDate:(NSDate *)aDate{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];//gets the year, month, and day for today's date
    NSDate *date = [calendar dateFromComponents:components]; // makes a new NSDate keeping only the year, month, and day
    return date;
}

@end
